package xray

import (
	"fmt"
	"gitee.com/gam6it/SecToolkit/utils/http2"
)

const (
	IsAliveTrue         = "ALIVE"
	IsAliveUnknown      = "UNKNOWN"
	AssetSeverityNone   = "NONE"
	AssetSeverityHigh   = "HIGH"
	AssetSeverityMedium = "MEDIUM"
	AssetSeverityLow    = "LOW"
)

type Host struct {
	Id                  int          `json:"id"`
	CreatedTime         string       `json:"created_time"`
	UpdatedTime         string       `json:"updated_time"`
	IsAlive             string       `json:"is_alive"`
	LastSurvivalTime    string       `json:"last_survival_time"`
	Origin              string       `json:"origin"`
	Address             string       `json:"address"`
	Information         string       `json:"information"`
	Remark              string       `json:"remark"`
	Project             Project      `json:"project"`
	Weight              int          `json:"weight"`
	VulnIds             []int        `json:"vuln_ids"`
	XprocessIds         []int        `json:"xprocess_ids"`
	AssetSeverity       string       `json:"asset_severity"`
	Operator            string       `json:"operator"`
	InternetGeolocation string       `json:"internet_geolocation"`
	DeviceType          []DeviceType `json:"device_type"`
	MacAddress          string       `json:"mac_address"`
}

type HostFilterOption struct {
	Limit             int            `json:"limit"`
	Offset            int            `json:"offset"`
	Origin            []string       `json:"origin,omitempty"`
	Remark            []FilterOption `json:"remark,omitempty"`
	IP                []FilterOption `json:"ip,omitempty"`
	Information       []FilterOption `json:"information,omitempty"`
	IsAlive           []string       `json:"is_alive,omitempty"`
	LastSurvivalTime  *TimeRange     `json:"last_survival_time,omitempty"`
	UpdatedTime       *TimeRange     `json:"updated_time,omitempty"`
	HostIds           []int          `json:"host_ids,omitempty"`
	BusinessSystemIds []int          `json:"business_system_ids,omitempty"`
	NetworkRegionIds  []int          `json:"network_region_ids,omitempty"`
	TagIds            []int          `json:"tag_ids,omitempty"`
	LocationIds       []int          `json:"location_ids,omitempty"`
}

func (c *Client) HostFilter(options ...HostFilterOption) (int64, []Host, error) {
	var option = HostFilterOption{
		Limit:  0,
		Offset: 0,
	}
	if len(options) == 1 {
		option = options[0]
	}
	type resp struct {
		Total   int64  `json:"total"`
		Content []Host `json:"content"`
	}
	var result resp
	api := fmt.Sprintf("%sapi/v2/ip/filter/?project_id=%d", c.URI, c.ProjectId)
	err := request(http2.MethodPost, api, c.Token, &result, http2.JsonParam(option), http2.TimeOut(c.TimeOut))
	return result.Total, result.Content, err
}
